# Project: Serbia Survey
# Authors: William O'Brochta and Patrick Cunha Silva
# Goal: Load Libraries and Custom Functions

rm(list = ls())

# Load Libraries
library(lmtest)
library(sandwich)
library(stargazer)
library(fastDummies)
library(here)
library(quanteda)
library(chron) 
library(stm) 
library(fastDummies)

##### Function to plot interaction
plot_interaction <- function(model = m1a, x = 'cyrillic_second', z = 'cyrillic_second:intPolitics', z_values = 1:5, se_type = 'HC2', x_name = 'Alphabet', z_name = 'Interest in Politics'){
  # betas
  betas <- coef(model)[x] + coef(model)[z] * z_values
  # vcov 
  vcov_matrix <- vcovHC(model, type = se_type)
  # SE
  ses <- sqrt(vcov_matrix[x, x] + z_values^2*vcov_matrix[z, z] + 2*z_values*vcov_matrix[x, z])
  # 95 CI, UB
  ub <- qt(0.975, df = df.residual(model)) * ses + betas
  # 95 CI, LB
  lb <- qt(0.025, df = df.residual(model)) * ses + betas  
  # Plot
  par(mar = c(6, 6, 2, 2))
  plot(x = z_values,
       y = betas, type = 'p', pch = 19,
       ylim = c(round(min(lb) - 0.5), round(max(ub) + 0.5)), 
       xlim = c(min(z_values), max(z_values)), axes = FALSE, 
       xlab = '', ylab = '', cex.lab = 2)
  mtext(paste0('Marginal Effect of ', x_name), side = 2, line = 4, cex = 2)
  mtext(z_name, side = 1, line = 4, cex = 2)
  axis(1, cex.axis = 2, at = z_values)
  axis(2, cex.axis = 2, las = 2)
  for(i in 1:length(z_values)) lines(x = c(z_values[i], z_values[i]), y = c(ub[i], lb[i]))
  abline(h = 0, lwd = 0.5, lty = 2)
}



################ R version used for the original code:
# R version 4.3.0 (2023-04-21)
# Platform: aarch64-apple-darwin22.4.0 (64-bit)
# Running under: macOS Ventura 13.4
# 
# Matrix products: default
# BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib 
# LAPACK: /opt/homebrew/Cellar/r/4.3.0_1/lib/R/lib/libRlapack.dylib;  LAPACK version 3.11.0
# 
# locale:
#   [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
# 
# time zone: America/Chicago
# tzcode source: internal
# 
# attached base packages:
#   [1] stats     graphics  grDevices utils     datasets  methods   base     
# 
# other attached packages:
#   [1] stm_1.3.6         chron_2.3-61      quanteda_3.3.1    here_1.0.1        fastDummies_1.6.3 stargazer_5.2.3   sandwich_3.0-2    lmtest_0.9-40    
# [9] zoo_1.8-12       
# 
# loaded via a namespace (and not attached):
#   [1] fastmatch_1.1-3    Matrix_1.5-4       lattice_0.21-8     magrittr_2.0.3     RcppParallel_5.1.7 stopwords_2.3      grid_4.3.0         data.table_1.14.8 
# [9] compiler_4.3.0     rprojroot_2.0.3    tools_4.3.0        Rcpp_1.0.10        stringi_1.7.12    


################ R version used for the R&R
# > sessionInfo()
# R version 4.3.0 (2023-04-21)
# Platform: aarch64-apple-darwin22.4.0 (64-bit)
# Running under: macOS Ventura 13.5.2
# 
# Matrix products: default
# BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib 
# LAPACK: /opt/homebrew/Cellar/r/4.3.0_1/lib/R/lib/libRlapack.dylib;  LAPACK version 3.11.0
# 
# locale:
#   [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
# 
# time zone: America/Chicago
# tzcode source: internal
# 
# attached base packages:
#   [1] stats     graphics  grDevices utils     datasets  methods   base     
# 
# other attached packages:
#   [1] stm_1.3.6         chron_2.3-61      quanteda_3.3.1    here_1.0.1        fastDummies_1.6.3 stargazer_5.2.3   sandwich_3.0-2    lmtest_0.9-40    
# [9] zoo_1.8-12       
# 
# loaded via a namespace (and not attached):
#   [1] fastmatch_1.1-3    Matrix_1.5-4       lattice_0.21-8     magrittr_2.0.3     RcppParallel_5.1.7 stopwords_2.3      grid_4.3.0         data.table_1.14.8 
# [9] compiler_4.3.0     rprojroot_2.0.3    rstudioapi_0.14    tools_4.3.0        Rcpp_1.0.10        stringi_1.7.12    
